package com.frank.oj.controller.file;

import com.frank.model.entity.group.Group;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.service.file.ImageService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author frank
 * @Date 2024/3/28
 */
@RestController
@RequiresAuthentication
@RequestMapping("/api/file")
public class ImageController {
    @Resource
    private ImageService imageService;

    @PostMapping("/upload-group-avatar")
    public CommonResult<Group> uploadGroupAvatar(@RequestParam(value = "image", required = true) MultipartFile image,
                                                 @RequestParam(value = "gid", required = true) Long gid) {
        return imageService.uploadGroupAvatar(image, gid);
    }

    /**
     * 轮播图上传
     *
     * @param image 文件
     * @return 路径
     */
    @PostMapping("/upload-carouse-img")
    @RequiresRoles("root")
    public CommonResult<Map<Object, Object>> uploadCarouselImg(@RequestParam("file") MultipartFile image) {
        return imageService.uploadCarouselImg(image);
    }

    /**
     * 上传头像
     *
     * @param image 文件
     * @return 路径
     */
    @PostMapping("/upload-avatar")
    @RequiresAuthentication
    public CommonResult<Map<Object, Object>> uploadAvatar(@RequestParam("image") MultipartFile image) {
        return imageService.uploadAvatar(image);
    }
}
